package tool.expand;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ScopedMap<K, V> implements Map<K, V> {
	private ArrayList<Map<K, V>> stack = new ArrayList<Map<K, V>>();

	@Override
	public void clear() {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean containsKey(Object key) {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean containsValue(Object value) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Set<java.util.Map.Entry<K, V>> entrySet() {
		throw new UnsupportedOperationException();
	}

	@Override
	public V get(Object key) {
		V result = null;
		for (int i=stack.size()-1 ; i>=0 ; i--) {
			result = stack.get(i).get(key);
			if (result != null) break;
		}
		return result;
	}

	@Override
	public boolean isEmpty() {
		throw new UnsupportedOperationException();
	}

	@Override
	public Set<K> keySet() {
		throw new UnsupportedOperationException();
	}

	@Override
	public V put(K key, V value) {
		stack.get(stack.size()-1).put(key, value);
		return null;
	}

	@Override
	public void putAll(Map<? extends K, ? extends V> m) {
		stack.get(stack.size()-1).putAll(m);
	}

	@Override
	public V remove(Object key) {
		throw new UnsupportedOperationException();
	}

	@Override
	public int size() {
		throw new UnsupportedOperationException();
	}

	@Override
	public Collection<V> values() {
		throw new UnsupportedOperationException();
	}
	public void addScope() {
		stack.add(new HashMap<K, V>());
	}
	public void addScope(Map<K, V> map) {
		stack.add(map);
	}
	public void removeScope() {
		stack.remove(stack.size()-1);
	}
}
